package me.zhengjie.utils;


import com.alibaba.excel.EasyExcel;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: chenxin.jiang
 * @Date: 2020/12/17
 * @Description:Excel工具类
 */
public class ExcelUtil {
    //导出Excel
    public static void download(HttpServletResponse response, Class t, List list) throws IOException, IllegalAccessException,InstantiationException {
        response.setContentType("application/vnd.ms-excel");// 设置文本内省
        response.setCharacterEncoding("utf-8");// 设置字符编码
        SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = format.format(new Date());
        StringBuffer s1 =new StringBuffer();
        s1.append("attachment;filename=").append("tableExport-"+fileName).append(".xlsx");
        response.setHeader("Content-disposition", s1.toString()); // 设置响应头
        EasyExcel.write(response.getOutputStream(), t).sheet().doWrite(list); //用io流来写入数据
    }
}
